package revenge.jack_cristo.shiyuanbaoyou;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import revenge.jack_cristo.shiyuanbaoyou.adapter.Adapter_Shouye;
import revenge.jack_cristo.shiyuanbaoyou.bean.Shouye_gengxin;
import revenge.jack_cristo.shiyuanbaoyou.bean.Shouye_listbean;
import revenge.jack_cristo.shiyuanbaoyou.bean.Shouye_lunbo_adbean;
import revenge.jack_cristo.shiyuanbaoyou.customview.MyGridview;
import revenge.jack_cristo.shiyuanbaoyou.main_interface.Shouye_interface;
import revenge.jack_cristo.shiyuanbaoyou.utils.GlideImageLoader;

/**
 * Created by Jack-Cristo on 2016/11/11.
 */

public class Shouye_fragment extends Fragment {
    private static Context context = null;
    private Adapter_Shouye adapter;
    private String baseurl_lunbo = "http://www.syby8.com/apptools/";
    private List<String> listgengxinurl = new ArrayList<>();
    private List<String> listlunbourl = new ArrayList<>();
    private List<Shouye_listbean.RowsBean> list_product = new ArrayList<>();
    private Banner banner;
    private Button nvzhuang, jujia, xiebao, gengduo;
    private ImageView jinrigengxin, shouji, meishihui, meizhuang, zuihou;
    private Toolbar toolbar;
    private MyGridview gridView;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    banner.start();
                    break;
                case 1:
                    initgengxinview();
                    break;
                case 2:
                    gridView.setAdapter(adapter);
                    break;

            }
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.shouye_framelayout, container, false);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        toolbar = (Toolbar) view.findViewById(R.id.toolbar_shouye);
        toolbar.setNavigationIcon(R.mipmap.search);
        banner = (Banner) view.findViewById(R.id.fragment_banner);
        nvzhuang = (Button) view.findViewById(R.id.nvzhuang_shouye);
        jujia = (Button) view.findViewById(R.id.jujia_shouye);
        xiebao = (Button) view.findViewById(R.id.xiebao_shouye);
        gengduo = (Button) view.findViewById(R.id.more_shouye);
        jinrigengxin = (ImageView) view.findViewById(R.id.jinrigengxin);
        shouji = (ImageView) view.findViewById(R.id.shoujizhoubian);
        meishihui = (ImageView) view.findViewById(R.id.meishihui);
        meizhuang = (ImageView) view.findViewById(R.id.meizhuanggehu);
        zuihou = (ImageView) view.findViewById(R.id.zuihoufengqiang);
        gridView = (MyGridview) view.findViewById(R.id.gride_shouye);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initlunbodata();
        initlunboview();
        initgengxindata();
        initgridviewdata();
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


                Intent intent = new Intent();
                intent.putExtra("weburl", list_product.get(i).getProductUrl());
                intent.setClass(context, WebviewActivity.class);
                startActivity(intent);


            }
        });

    }

    private void initgridviewdata() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseurl_lunbo)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Shouye_interface shouye_interface = retrofit.create(Shouye_interface.class);
        Call<Shouye_listbean> call = shouye_interface.getlist();
        call.enqueue(new Callback<Shouye_listbean>() {
            @Override
            public void onResponse(Call<Shouye_listbean> call, Response<Shouye_listbean> response) {
                if (response.isSuccessful()) {
                    list_product = response.body().getRows();
                    adapter = new Adapter_Shouye(context, list_product);
                }
                handler.sendEmptyMessage(2);

            }

            @Override
            public void onFailure(Call<Shouye_listbean> call, Throwable t) {

            }
        });
    }


    private void initgengxinview() {
        if (listgengxinurl.size() == 5) {
            Glide.with(context).load(listgengxinurl.get(0)).into(jinrigengxin);
            Glide.with(context).load(listgengxinurl.get(1)).into(shouji);
            Glide.with(context).load(listgengxinurl.get(2)).into(meishihui);
            Glide.with(context).load(listgengxinurl.get(3)).into(meizhuang);
            Glide.with(context).load(listgengxinurl.get(4)).into(zuihou);
        } else {
            Glide.with(context).load(listgengxinurl.get(0)).into(jinrigengxin);
        }
    }

    private void initlunboview() {
        banner.setBannerStyle(BannerConfig.NUM_INDICATOR);
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(listlunbourl);
        //设置标题集合（当banner样式有显示title时）
        //banner.setBannerTitles(title_datu);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(3000);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.NUM_INDICATOR);


    }

    private void initgengxindata() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseurl_lunbo)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Shouye_interface shouye_interface = retrofit.create(Shouye_interface.class);
        Call<List<Shouye_gengxin>> call = shouye_interface.getgengxin();
        call.enqueue(new Callback<List<Shouye_gengxin>>() {
            @Override
            public void onResponse(Call<List<Shouye_gengxin>> call, Response<List<Shouye_gengxin>> response) {
                if (response.isSuccessful()) {
                    List<Shouye_gengxin> lisy_gengxin = response.body();
                    for (int i = 0; i < lisy_gengxin.size(); i++) {
                        listgengxinurl.add(lisy_gengxin.get(i).getImgUrl());
                    }

                    handler.sendEmptyMessage(1);
                }
            }

            @Override
            public void onFailure(Call<List<Shouye_gengxin>> call, Throwable t) {

            }
        });

    }

    public void initlunbodata() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseurl_lunbo)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Shouye_interface shouye_inf = retrofit.create(Shouye_interface.class);
        Call<Shouye_lunbo_adbean> call = shouye_inf.getlunboad();
        call.enqueue(new Callback<Shouye_lunbo_adbean>() {
            @Override
            public void onResponse(Call<Shouye_lunbo_adbean> call,
                                   Response<Shouye_lunbo_adbean> response) {
                if (response.isSuccessful()) {
                    List<Shouye_lunbo_adbean.AdListBean> list_lunbo = response.body().getAdList();
                    for (int i = 0; i < list_lunbo.size(); i++) {
                        listlunbourl.add(list_lunbo.get(i).getImgUrl());

                    }
                    handler.sendEmptyMessage(0);
                }
            }

            @Override
            public void onFailure(Call<Shouye_lunbo_adbean> call, Throwable t) {
            }
        });
    }

    public static Shouye_fragment newInstance(Context con) {
        context = con;
        Bundle args = new Bundle();
        Shouye_fragment fragment = new Shouye_fragment();
        fragment.setArguments(args);
        return fragment;
    }
}
